﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using Palsta.Core.manifest;
using Palsta.Core.data;
using Palsta.Core.view;
using Palsta.Core.parser;

namespace Palsta.Core
{
    class InfoPathFormParser
    {
        private InfoPathForm form;

        public InfoPathForm Parse(String path)
        {
            this.form = new InfoPathForm();

            //parse mainfest.xsf
            ManifestParser mfp = new ManifestParser();
            Manifest mf = mfp.Parse(path + "manifest.xsf");
            if (mf == null)
            {
                return null;
            }
            this.form.XsnManifest = mf;

            //parse sampledata.xml & myschema.xsd
            DataDirectoryParser ddp = new DataDirectoryParser();
            DataDictionary dd = ddp.Parse(path + "sampledata.xml", path + "myschema.xsd");
            if (dd == null)
            {
                return null;
            }
            this.form.XsnDataDictionary = dd;


            //parse view
            foreach (ViewDesc vd in mf.Views)
            {
                String viewPath = path  + vd.Transform;
                ViewParser vp = new ViewParser();
                View v = vp.Parse(viewPath);
                if (v == null)
                {
                    return null;
                }
                v.Name = vd.Transform;
                this.form.XsnViews.Add(v);
            }

            //analyze test for views
            if (!this.parseTests())
            {
                return null;
            }

            //analyze expression in manifest.xsf
            if (!this.parseExpressions())
            {
                return null;
            }

            return this.form;

        }

        private Boolean parseExpressions()
        {
            return true;
        }

        private Boolean parseTests()
        {
            return true;
        }
    }
}
